{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Jupyter笔记本中的可重现性问题\n",
    "\n",
    "这个笔记本演示了Jupyter中常见的可重现性问题：\n",
    "1. 隐藏状态问题\n",
    "2. 乱序执行问题\n",
    "3. 幽灵变量问题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题1：隐藏状态\n",
    "\n",
    "在Jupyter中，你可以按任意顺序执行单元格，导致代码和输出不匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第一步：定义变量\n",
    "x = 10\n",
    "print(f\"x = {x}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第二步：使用变量\n",
    "y = x * 2\n",
    "print(f\"y = x * 2 = {y}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第三步：修改x的值\n",
    "x = 20\n",
    "print(f\"现在 x = {x}\")\n",
    "print(f\"但是 y 仍然是 {y}，因为第二个单元格没有重新运行\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题2：删除单元格后的幽灵变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个临时变量\n",
    "temp_variable = \"我是一个临时变量\"\n",
    "print(temp_variable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用临时变量\n",
    "result = f\"处理结果：{temp_variable}\"\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**问题演示**：\n",
    "1. 运行上面两个单元格\n",
    "2. 删除第一个单元格（定义temp_variable的那个）\n",
    "3. 重新运行第二个单元格 - 它仍然能工作，因为temp_variable还在内存中\n",
    "4. 但是如果有人重新启动内核并运行笔记本，第二个单元格会失败"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题3：依赖关系不明确"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 创建数据\n",
    "data = pd.DataFrame({\n",
    "    'A': np.random.randn(100),\n",
    "    'B': np.random.randn(100)\n",
    "})\n",
    "print(\"数据已创建\")\n",
    "print(data.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据处理\n",
    "processed_data = data.copy()\n",
    "processed_data['C'] = processed_data['A'] + processed_data['B']\n",
    "print(f\"处理后的数据形状：{processed_data.shape}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分析结果\n",
    "mean_c = processed_data['C'].mean()\n",
    "print(f\"C列的平均值：{mean_c}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**问题演示**：\n",
    "1. 如果你修改第一个单元格中的数据生成逻辑\n",
    "2. 后续单元格不会自动重新运行\n",
    "3. 导致分析结果基于旧数据，但看起来像是基于新数据"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "marimo-ui (3.12.11)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
